package com.fupan.controller;


import com.fupan.entity.Booktype;
import com.fupan.service.BooktypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author stec
 * @since 2023-02-13 04:28:56
 */
@RestController
@RequestMapping("/booktype")
public class BooktypeController {


    @Autowired
    BooktypeService booktypeService;

    @Autowired
    RedisTemplate<String ,Object> stringObjectRedisTemplate;

    @RequestMapping("all")
//    @Cacheable(value = "bookType",key = "#root.targetClass+#root.methodName")
    public List<Booktype> all() {

        ValueOperations<String, Object> stringObjectValueOperations = stringObjectRedisTemplate.opsForValue();
        Object bookType = stringObjectValueOperations.get("bookType");
        if (bookType == null) {
            List<Booktype> booktypes = booktypeService.list();
            stringObjectValueOperations.set("bookType", booktypes);
            return booktypes;
        } else {
            return (List<Booktype>) bookType;
        }


    }

}

